package com.bupt.smart.network;

import com.bupt.smart.network.api.DeliveryAPI;
import com.bupt.smart.network.api.MarketAPI;
import com.bupt.smart.network.api.QiNiuCloudAPI;
import com.bupt.smart.network.api.RepairAPI;
import com.bupt.smart.network.api.UserAPI;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by ght on 2016/4/5.
 */
public class APIManager {
    private static final String API_BASE_URL = "http://10.108.100.81:8080/SmartCommunity/";
    private static Retrofit retrofit = null;
    private static UserAPI userAPI = null;
    private static RepairAPI repairAPI = null;
    private static QiNiuCloudAPI qiNiuCloudAPI = null;
    private static DeliveryAPI deliveryAPI = null;
    private static MarketAPI marketAPI = null;

    public static Retrofit getRetrofit() {
        if (retrofit == null) {
            synchronized (APIManager.class) {
                if (retrofit == null) {
                    Gson gson = new GsonBuilder()
                            .setDateFormat("yyyy-MM-dd HH:mm:ss")
                            .create();

                    retrofit = new Retrofit.Builder()
                            .baseUrl(API_BASE_URL)
                            .addConverterFactory(GsonConverterFactory.create(gson))
                            .build();
                }
            }
        }
        return retrofit;
    }

    public static UserAPI getUserAPI() {
        if (userAPI == null) {
            synchronized (APIManager.class) {
                if (userAPI == null) {
                    userAPI = getRetrofit().create(UserAPI.class);
                }
            }
        }
        return userAPI;
    }

    public static RepairAPI getRepairAPI() {
        if (repairAPI == null) {
            synchronized (APIManager.class) {
                if (repairAPI == null) {
                    repairAPI = getRetrofit().create(RepairAPI.class);
                }
            }
        }
        return repairAPI;
    }

    public static QiNiuCloudAPI getQiNiuCloudAPI() {
        if (qiNiuCloudAPI == null) {
            synchronized (QiNiuCloudAPI.class) {
                if (qiNiuCloudAPI == null) {
                    qiNiuCloudAPI = getRetrofit().create(QiNiuCloudAPI.class);
                }
            }
        }
        return qiNiuCloudAPI;
    }

    public static DeliveryAPI getDeliveryAPI() {
        if (deliveryAPI == null) {
            synchronized (DeliveryAPI.class) {
                if (deliveryAPI == null) {
                    deliveryAPI = getRetrofit().create(DeliveryAPI.class);
                }
            }
        }
        return deliveryAPI;
    }
    public static MarketAPI getMarketAPI(){
        if(marketAPI == null) {
            synchronized (MarketAPI.class) {
                if(marketAPI == null) {
                    marketAPI = getRetrofit().create(MarketAPI.class);
                }
            }
        }
        return marketAPI;
    }
}
